
/**
 * Created with IntelliJ IDEA
 * Description  无头单向非循环链表实现
 * User: 王杰
 * Date: 2025-05-26
 * Time: 20:33
 */
public class MySingleList implements IList{
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    // 创建链表
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    // 显示方法
    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    // 链表大小
    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    // 链表是否存在 key 值
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = head;
        head = node;
    }

    // 尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        // 一个节点都没有
        if (head == null) {
            head = node;
            return;
        }

        // 找尾巴
        ListNode cur = head;
        while (cur != null) {
            if (cur.next == null) {
                cur.next = node;
                return;
            }
            cur = cur.next;
        }
    }

    //中间插入
    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if (index < 0 || index > len) {
            System.out.println("index位置不存在");
            return;
        }

        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == len) {
            addLast(data);
            return;
        }

        // 中间插入
        ListNode cur = head;
        if (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        // 所有的插入 优先 绑定后边
        node.next = cur.next;
        cur.next = node;

    }



    // 删除 key值 节点
    @Override
    public void remove(int key) {
        if (head == null) {
            return;
        }

        // 删除头节点
        if (head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findNodeOfKey(key);
        if (cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findNodeOfKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    // 删除 所有key值 节点
    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

        if (head.val == key) {
            head = head.next;
        }
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }



}
